﻿ 
// Generated File 
// Generated: 13-6-2010 14:57:40
namespace PersonLookup.ViewModel
{
	public partial class Person : System.ComponentModel.INotifyPropertyChanged, System.ComponentModel.IEditableObject
	{
		#region Properties
		
		#region ID Property
		
		private int _iD;
		public int ID
		{
			get
			{
				return _iD;
			}
			set
			{
				if (_iD != value)
				{
					_iD = value;
					OnPropertyChanged("ID");
				}
			}
		}
		
		#endregion
		#region FirstName Property
		
		private string _firstName;
		public string FirstName
		{
			get
			{
				return _firstName;
			}
			set
			{
				if (_firstName != value)
				{
					_firstName = value;
					OnPropertyChanged("FirstName");
				}
			}
		}
		
		#endregion
		#region LastName Property
		
		private string _lastName;
		public string LastName
		{
			get
			{
				return _lastName;
			}
			set
			{
				if (_lastName != value)
				{
					_lastName = value;
					OnPropertyChanged("LastName");
				}
			}
		}
		
		#endregion
		#region EntityStatus Property
		
		private MVVM.EntityStatus _entityStatus;
		public MVVM.EntityStatus EntityStatus
		{
			get
			{
				return _entityStatus;
			}
			set
			{
				if (_entityStatus != value)
				{
					_entityStatus = value;
					OnPropertyChanged("EntityStatus");
				}
			}
		}
		
		#endregion

		#endregion	
		
		public static Person FromModel(PersonLookup.PersonServiceReference.Person modelEntity)
		{
			var viewModelEntity = new PersonLookup.ViewModel.Person();
			viewModelEntity.ID = modelEntity.ID;
			viewModelEntity.FirstName = modelEntity.FirstName;
			viewModelEntity.LastName = modelEntity.LastName;
			return viewModelEntity;
		}
		
		public PersonLookup.PersonServiceReference.Person ToModel()
		{
			var modelEntity = new PersonLookup.PersonServiceReference.Person();
			modelEntity.ID = this.ID;
			modelEntity.FirstName = this.FirstName;
			modelEntity.LastName = this.LastName;
			return modelEntity;
		}

		public Person()
		{
			SaveCommand = new global::MVVM.ActionCommand(SaveCommand_Execute);
			UpdateCommand = new global::MVVM.ActionCommand(UpdateCommand_Execute);
			DeleteCommand = new global::MVVM.ActionCommand(DeleteCommand_Execute);
			BeginEditCommand = new global::MVVM.ActionCommand(BeginEditCommand_Execute);
			CancelEditCommand = new global::MVVM.ActionCommand(CancelEditCommand_Execute);
			EndEditCommand = new global::MVVM.ActionCommand(EndEditCommand_Execute);
			ConstructorExecuting();
		}
		
		partial void ConstructorExecuting();
		#region Commands
		
		#region SaveCommand Command
		
		public System.Windows.Input.ICommand SaveCommand
		{
			get;
			private set;
		}
		
		public void SaveCommand_Execute(object parameter)
		{
			var viewModelProvider = new PersonLookup.ViewModel.Providers.PersonProvider();
			viewModelProvider.SaveCompleted += viewModelProvider_SaveCompleted;
			viewModelProvider.SaveAsync(this);
		}
		
		partial void SaveCompleted(PersonLookup.ViewModel.Person person, System.Exception error, object userState);
		
		public void viewModelProvider_SaveCompleted(object sender, PersonLookup.ViewModel.Providers.PersonProvider.SaveCompletedEventArgs e)
		{
			SaveCompleted(e.Result, e.Error, e.UserState);
		}
		
		#endregion
		
		#region UpdateCommand Command
		
		public System.Windows.Input.ICommand UpdateCommand
		{
			get;
			private set;
		}
		
		public void UpdateCommand_Execute(object parameter)
		{
			var viewModelProvider = new PersonLookup.ViewModel.Providers.PersonProvider();
			viewModelProvider.UpdateCompleted += viewModelProvider_UpdateCompleted;
			viewModelProvider.UpdateAsync(this);
		}
		
		partial void UpdateCompleted(int rowsAffected, System.Exception error, object userState);
		
		public void viewModelProvider_UpdateCompleted(object sender, PersonLookup.ViewModel.Providers.PersonProvider.UpdateCompletedEventArgs e)
		{
			UpdateCompleted(e.Result, e.Error, e.UserState);
		}
		
		#endregion
		
		#region DeleteCommand Command
		
		public System.Windows.Input.ICommand DeleteCommand
		{
			get;
			private set;
		}
		
		public void DeleteCommand_Execute(object parameter)
		{
			var viewModelProvider = new PersonLookup.ViewModel.Providers.PersonProvider();
			viewModelProvider.DeleteCompleted += viewModelProvider_DeleteCompleted;
			viewModelProvider.DeleteAsync(this);
		}
		
		partial void DeleteCompleted(int rowsAffected, System.Exception error, object userState);
		
		public void viewModelProvider_DeleteCompleted(object sender, PersonLookup.ViewModel.Providers.PersonProvider.DeleteCompletedEventArgs e)
		{
			DeleteCompleted(e.Result, e.Error, e.UserState);
		}
		
		#endregion
		
		#region BeginEditCommand Command
		
		public System.Windows.Input.ICommand BeginEditCommand
		{
			get;
			private set;
		}
		
		public void BeginEditCommand_Execute(object parameter)
		{
			BeginEdit();
		}
		#endregion
		
		#region CancelEditCommand Command
		
		public System.Windows.Input.ICommand CancelEditCommand
		{
			get;
			private set;
		}
		
		public void CancelEditCommand_Execute(object parameter)
		{
			CancelEdit();
		}
		#endregion
		
		#region EndEditCommand Command
		
		public System.Windows.Input.ICommand EndEditCommand
		{
			get;
			private set;
		}
		
		public void EndEditCommand_Execute(object parameter)
		{
			EndEdit();
		}
		#endregion
		#endregion
		
		#region IEditableObject Implementation
		
		private int _oldID;
		private string _oldFirstName;
		private string _oldLastName;
		private MVVM.EntityStatus _oldEntityStatus;

		#region IsEditing Property
		
		private bool _isEditing;
		public bool IsEditing
		{
			get
			{
				return _isEditing;
			}
			set
			{
				if (_isEditing != value)
				{
					_isEditing = value;
					OnPropertyChanged("IsEditing");
				}
			}
		}
		
		#endregion
		
		
		partial void BeforeBeginEdit(ref bool cancel);
		partial void AfterBeginEdit();
		public void BeginEdit()
		{
			bool cancel= false;
			BeforeBeginEdit(ref cancel);
			if(!cancel)
			{
			_oldID = ID;
			_oldFirstName = FirstName;
			_oldLastName = LastName;
			_oldEntityStatus = EntityStatus;
				IsEditing = true;
				AfterBeginEdit();
			}
		}

		partial void BeforeCancelEdit(ref bool cancel);
		partial void AfterCancelEdit();
		
		public void CancelEdit()
		{
			bool cancel = false;
			BeforeCancelEdit(ref cancel);
			if(!cancel)
			{
				ID = _oldID;
				FirstName = _oldFirstName;
				LastName = _oldLastName;
				EntityStatus = _oldEntityStatus;
				IsEditing = false;
				AfterCancelEdit();
			}
		}

		partial void BeforeEndEdit(ref bool cancel);
		partial void AfterEndEdit();
		
		public void EndEdit()
		{
			bool cancel = false;
			BeforeEndEdit(ref cancel);
			if(!cancel)
			{
				IsEditing = false;
				AfterEndEdit();
			}
		}
		
		#endregion
	
		#region INotifyPropertyChanged Implementation
		
		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		protected void OnPropertyChanged(string propertyName)
		{
			System.ComponentModel.PropertyChangedEventHandler p = PropertyChanged;
			if (p != null)
			{
				p(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
			}
		}
		
		#endregion
	}
}

namespace PersonLookup.ViewModel
{
	public partial class PersonCollection : System.Collections.ObjectModel.ObservableCollection<PersonLookup.ViewModel.Person>
	{
		public static PersonCollection FromModel(System.Collections.Generic.IEnumerable<PersonLookup.PersonServiceReference.Person> modelCollection)
		{
			var viewModelCollection = new PersonCollection();
			foreach(var item in modelCollection)
			{
				viewModelCollection.Add(PersonLookup.ViewModel.Person.FromModel(item));
			}
			return viewModelCollection;
		}
		
		public System.Collections.Generic.IEnumerable<PersonLookup.PersonServiceReference.Person> ToModel()
		{
			var modelCollection = new System.Collections.Generic.List<PersonLookup.PersonServiceReference.Person>();
			foreach(var item in this)
			{
				modelCollection.Add(item.ToModel());
			}
			return modelCollection;
		}
	}
}

			
namespace PersonLookup.ViewModel.Providers
{
	public partial class PersonProvider
	{
		#region Save Implementation
		
		public class SaveCompletedEventArgs : System.EventArgs
		{
			public PersonLookup.ViewModel.Person Result
			{
				get;
				private set;
			}
			
			public System.Exception Error
			{
				get;
				private set;
			}
			
			public object UserState
			{
				get;
				private set;
			}
			
			public SaveCompletedEventArgs(PersonLookup.ViewModel.Person result, System.Exception error, object userState)
			{
				Result = result;
				Error = error;
				UserState = userState;
			}
		}
		
		public event System.EventHandler<SaveCompletedEventArgs> SaveCompleted;
	
		public void SaveAsync(PersonLookup.ViewModel.Person person, object userState = null)
		{
			var modelProvider = new PersonLookup.PersonServiceReference.PersonServiceClient();
			var modelEntity = person.ToModel();
			modelProvider.AddPersonCompleted += PersonProvider_AddPersonCompleted;
			modelProvider.AddPersonAsync(modelEntity, userState);
		}
		
		public void PersonProvider_AddPersonCompleted(object sender, PersonLookup.PersonServiceReference.AddPersonCompletedEventArgs e)
		{
			System.EventHandler<SaveCompletedEventArgs> ev = SaveCompleted;
			if(ev != null)
			{
		
				var result = PersonLookup.ViewModel.Person.FromModel(e.Result);
		
				ev(this, new SaveCompletedEventArgs(result, e.Error, e.UserState));
			}
		}
		
		#endregion
		
		#region Update Implementation
		
		public class UpdateCompletedEventArgs : System.EventArgs
		{
			public int Result
			{
				get;
				private set;
			}
			
			public System.Exception Error
			{
				get;
				private set;
			}
			
			public object UserState
			{
				get;
				private set;
			}
			
			public UpdateCompletedEventArgs(int result, System.Exception error, object userState)
			{
				Result = result;
				Error = error;
				UserState = userState;
			}
		}
		
		public event System.EventHandler<UpdateCompletedEventArgs> UpdateCompleted;
	
		public void UpdateAsync(PersonLookup.ViewModel.Person person, object userState = null)
		{
			var modelProvider = new PersonLookup.PersonServiceReference.PersonServiceClient();
			var modelEntity = person.ToModel();
			modelProvider.UpdatePersonCompleted += PersonProvider_UpdatePersonCompleted;
			modelProvider.UpdatePersonAsync(modelEntity, userState);
		}
		
		public void PersonProvider_UpdatePersonCompleted(object sender, PersonLookup.PersonServiceReference.UpdatePersonCompletedEventArgs e)
		{
			System.EventHandler<UpdateCompletedEventArgs> ev = UpdateCompleted;
			if(ev != null)
			{
				var result = e.Result;
		
				ev(this, new UpdateCompletedEventArgs(result, e.Error, e.UserState));
			}
		}
		
		#endregion
		
		#region Delete Implementation
		
		public class DeleteCompletedEventArgs : System.EventArgs
		{
			public int Result
			{
				get;
				private set;
			}
			
			public System.Exception Error
			{
				get;
				private set;
			}
			
			public object UserState
			{
				get;
				private set;
			}
			
			public DeleteCompletedEventArgs(int result, System.Exception error, object userState)
			{
				Result = result;
				Error = error;
				UserState = userState;
			}
		}
		
		public event System.EventHandler<DeleteCompletedEventArgs> DeleteCompleted;
	
		public void DeleteAsync(PersonLookup.ViewModel.Person person, object userState = null)
		{
			var modelProvider = new PersonLookup.PersonServiceReference.PersonServiceClient();
			var modelEntity = person.ToModel();
			modelProvider.DeletePersonCompleted += PersonProvider_DeletePersonCompleted;
			modelProvider.DeletePersonAsync(modelEntity, userState);
		}
		
		public void PersonProvider_DeletePersonCompleted(object sender, PersonLookup.PersonServiceReference.DeletePersonCompletedEventArgs e)
		{
			System.EventHandler<DeleteCompletedEventArgs> ev = DeleteCompleted;
			if(ev != null)
			{
				var result = e.Result;
		
				ev(this, new DeleteCompletedEventArgs(result, e.Error, e.UserState));
			}
		}
		
		#endregion
		
	}
}

